#!/bin/bash

set -eo pipefail

exists () {
  type "$1" >/dev/null 2>/dev/null
}

package_install () {
    local pacman_name=$1
    local apt_name=${2:-$pacman_name}
    local brew_name=${2:-$pacman_name}
    if exists yay; then
        yay -S --noconfirm --needed $pacman_name
    elif exists apt-get; then
        # todo
        return 1
    elif exists brew; then
        brew install $brew_name
    fi
}

setup_yay() {
    if exists yay; then
        return 0
    fi
    sudo pacman -Sy --noconfirm yay
}

setup_archlinuxcn() {
    if cat /etc/pacman.conf | grep archlinuxcn 1>/dev/null 2>/dev/null; then
        return 0
    fi
    cat << EOF | sudo tee -a /etc/pacman.conf > /dev/null

[archlinuxcn]
Server = https://mirrors.ustc.edu.cn/archlinuxcn/\$arch
EOF
    sudo pacman -Sy --noconfirm archlinuxcn-keyring
}

setup_homebrew() {
    if ! command -v brew; then
        /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
    else
        echo "brew already installed, skip"
    fi
}

install_required_packages() {
    package_install thefuck
    package_install neovim
    package_install tmux
    package_install zsh
    package_install yarn
    package_install ripgrep
    package_install fd
    package_install autojump
    package_install gpg
    package_install nodejs node
    package_install entr
    package_install asdf-vm
}

prepare_os() {
    if exists pacman; then
        setup_archlinuxcn
        setup_yay
    elif [ $(uname -s) = "Darwin" ]; then
        echo "You are using macOs"
        setup_homebrew
    fi
}

setup_gpg() {
    gpg --import ~/.config/my-gpg-sk.asc
}

setup_tmux() {
    # setup tpm

    if [ ! -f ~/.tmux/plugins/tpm/README.md ]; then
        git clone https://github.com/tmux-plugins/tpm ~/.tmux/plugins/tpm
        ~/.tmux/plugins/tpm/scripts/install_plugins.sh
    else
        echo "tpm already exists, skip"
    fi
}

setup_nvim() {
    # git clone --depth 1 https://github.com/AstroNvim/AstroNvim ~/.config/nvim
    # git clone https://github.com/felinae98/my-astronvim ~/.config/nvim/lua/user
    nvim  --headless -c 'quitall'
}

setup_fish() {
    if ! cat /etc/shells | grep fish >/dev/null 2>&1; then
        command -v fish | sudo tee -a /etc/shells > /dev/null
    fi
    user_name=$USER
    if [ $SHeLL != $(command -v fish) ]; then
        sudo chsh -s $(command -v fish) $user_name
    fi

    if [ ! -f ~/.local/share/omf/README.md ]; then
        curl https://raw.githubusercontent.com/oh-my-fish/oh-my-fish/master/bin/install | fish
    fi
}

setup_zimfw() {
    echo "Installing zimfw"
    zsh <<- 'EOF'
    ZIM_HOME=~/.zim
    if [[ ! -e ${ZIM_HOME}/zimfw.zsh ]]; then
        curl -fsSL --create-dirs -o ${ZIM_HOME}/zimfw.zsh \
	    https://github.com/zimfw/zimfw/releases/latest/download/zimfw.zsh
    fi
    # Install missing modules, and update ${ZIM_HOME}/init.zsh if missing or outdated.
	if [[ ! ${ZIM_HOME}/init.zsh -nt ${ZDOTDIR:-${HOME}}/.zimrc ]]; then
	  source ${ZIM_HOME}/zimfw.zsh init -q
	fi
	source ${ZIM_HOME}/init.zsh
EOF
}

setup_kitty() {
    git clone --depth 1 https://github.com/dexpota/kitty-themes.git ~/.config/kitty/kitty-themes
    ln -s ~/.config/kitty/kitty-themes/themes/Solarized_Dark.conf ~/.config/kitty/theme.conf
}

echo "Updating the yadm repo origin URL"
yadm remote set-url origin "git@gitee.com:felinae98/dotfiles.git"

yadm decrypt

prepare_os
install_required_packages
setup_gpg
setup_tmux
setup_nvim
# setup_fish
setup_kitty
setup_zimfw

# vim:ft=sh
